/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * Loan Class
 * 
 * Description: - 
 * 
 * 
 * @package shared
 * @author Li Yin (A0085686L)
 * @editor(s) Si Ling (A0085314J)
 */

package shared; 

import java.util.LinkedList;

public class Loan extends Record{
	protected Category cat;
	protected String lender;

	protected LinkedList<Expense> installments;
	protected int outstanding;
	protected boolean clear; //true when loan has been cleared
	protected LinkedList<Payment> futurePayments;
	private Record initial;

	//constructor
	public Loan(int _ID, String _name, int _amt, int _dd, int _mm, int _yy, String _detail,
			Category _cat, String _lender, Record _initial){
		ID=_ID;
		name=_name;
		yy=_yy;
		mm=_mm;
		dd=_dd;
		amt=_amt;
		detail=_detail;
		cat= _cat;
		lender= _lender;
		initial=_initial;

		installments=new LinkedList<Expense>();
		futurePayments= new LinkedList<Payment>();
		outstanding=_amt;
		clear=false;
	}

	//Added by Si Ling to String
	//Precondition: Loan name is not null, there is a loan
	//Postcondition: returns the name of the loan, date, amt and category
	@Override
	public String toString(){
		
		return super.toString();
		
	}

	//add methods
	public void addInstallment(Expense anInstallment) throws Exception{
		if(anInstallment == null){
			throw new Exception("Added null installment to loan");
		}
		installments.addLast(anInstallment);
		anInstallment.setLinkToLoan(this);
	}	
	public Payment addFuturePayment(int pAmt, int pdd, int pmm, int pyy){
		Payment aPayment = new Payment(pAmt, pdd, pmm, pyy);
		futurePayments.addLast(aPayment);
		return aPayment;
	}

	//Added by Si Ling
	public void deleteFuturePayments(Payment toBeDeleted){
		futurePayments.remove(toBeDeleted);
	}

	public void addFuturePayments(Payment toBeAdded){
		futurePayments.add(toBeAdded);
	}
	//**END**


	//getters
	public String getLender(){
		return lender;
	}
	public Category getCat(){
		return cat;
	}
	public LinkedList<Expense> getInstallments(){
		return installments;
	}
	public LinkedList<Payment> getFuturePayments(){
		return futurePayments;
	}
	public int getOutstanding(){
		//outstanding = amt - sum(installments)
		int totalPaid=0;
		for(int i=0;i<installments.size();i++){
			totalPaid+=installments.get(i).getAmt();
		}
		outstanding = amt - totalPaid;
		return outstanding;
	}
	public Record getInitial(){
		return initial;
	}
	public int getIniAmt(){
		return initial.getAmt();
	}
	public boolean isClear(){
		return clear;
	}

	//mutators
	public void modifyLender(String newLender){
		lender= newLender;
	}
	public void modifyCategory(Category newCat){
		cat = newCat;
	}

	public void deleteFuturePayment(int index){
		futurePayments.remove(index);
	}
	public void modifyIniAmt(int newIniAmt){
		initial.modifyAmt(newIniAmt);
	}
	public void clear(){
		clear=true;
	}
	public void unclear(){
		clear=false;
	}

}
